package com.example.web.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.web.SysConst;
import com.example.web.dto.*;
import com.example.web.dto.query.*;
import com.example.web.entity.*;
import com.example.web.mapper.*;
import com.example.web.service.*;
import com.example.web.tools.dto.*;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import lombok.SneakyThrows;
import com.example.web.tools.*;
/**
 * 商品属性功能实现类
 */
@Service
public class GoodPropsServiceImpl extends ServiceImpl<GoodPropsMapper, GoodProps> implements GoodPropsService {

	 /**
     * 操作数据库AppUser表mapper对象
     */
    @Autowired
    private AppUserMapper _AppUserMapper;
    /**
     * 操作数据库的GoodProps表mapper对象
     */
    @Autowired
    private GoodPropsMapper _GoodPropsMpper;

 			
    @Autowired
    private GoodMapper _GoodMapper;                        
			

    /**
     * 商品属性分页查询
     */
    @SneakyThrows
    @Override
    public PagedResult<GoodPropsDto> List(GoodPropsPagedInput input) {

      
        //声明一个支持商品属性查询的(拉姆达)表达式
        LambdaQueryWrapper<GoodProps> queryWrapper = Wrappers.<GoodProps>lambdaQuery()
                .eq(input.getId()!=null&&input.getId()!=0,GoodProps::getId,input.getId())
                .eq(input.getCreatorId()!=null,GoodProps::getCreatorId,input.getCreatorId());
      
     	 //如果前端搜索传入PropName不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getPropName())) {
            queryWrapper=queryWrapper.eq(GoodProps::getPropName,input.getPropName());
       	 }
     	 //如果前端搜索传入PropValue不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getPropValue())) {
            queryWrapper=queryWrapper.eq(GoodProps::getPropValue,input.getPropValue());
       	 }
     	 //如果前端搜索传入GoodId不为空,则进行精确查询
        if(input.getGoodId()!=null) {
            queryWrapper=queryWrapper.eq(GoodProps::getGoodId,input.getGoodId());
       	 }
      
        //按创建时间从大到小排序 最新的显示在最前面
        queryWrapper=queryWrapper.orderByDesc(GoodProps::getCreationTime);
        //构建一个分页查询的model
        Page<GoodProps> page = new Page<>(input.getPage(), input.getLimit());
          
   
         //从数据库进行分页查询获取商品属性数据
        IPage<GoodProps> pageRecords= _GoodPropsMpper.selectPage(page, queryWrapper);

        //获取所有满足条件的数据行数
        Long totalCount= _GoodPropsMpper.selectCount(queryWrapper);

        //把GoodProps实体转换成GoodProps传输模型
        List<GoodPropsDto> items= Extension.copyBeanList(pageRecords.getRecords(),GoodPropsDto.class);
        for (GoodPropsDto item : items) {
          
          //查询出关联的创建用户信息
            AppUserDto  CreatorAppUserDTO=new AppUserDto();
            AppUser  CreatorAppUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getCreatorId()));
           if(CreatorAppUserEntity!=null) {
                BeanUtils.copyProperties(CreatorAppUserDTO, CreatorAppUserEntity);
                item.setCreatorAppUserDto(CreatorAppUserDTO);
            }  
         
           
          	            
           //查询出关联的Good表信息
           GoodDto GoodDTO = new GoodDto();        
            Good  GoodEntity= _GoodMapper.selectOne(Wrappers.<Good>lambdaQuery().eq(Good::getId,item.getGoodId()));
           if(GoodEntity!=null) {  
          BeanUtils.copyProperties(GoodDTO,GoodEntity);
            item.setGoodDto(GoodDTO); 
           }
          
        }
      
        //返回一个分页结构给前端
        return PagedResult.GetInstance(items,totalCount);

    }
  
    /**
     * 单个商品属性查询
     */
    @SneakyThrows
    @Override
    public GoodPropsDto Get(GoodPropsPagedInput input) {
       if(input.getId()==null)
        {
            return new GoodPropsDto();
        }
        PagedResult<GoodPropsDto>  pagedResult =List(input);
        return pagedResult.getTotalCount()>0?pagedResult.getItems().stream().findFirst().get():new GoodPropsDto();
    }

    /**
     *商品属性创建或者修改
     */
    @SneakyThrows
    @Override
    public GoodPropsDto CreateOrEdit(GoodPropsDto input) {

        //声明一个商品属性实体
        GoodProps GoodProps = new GoodProps();

        //把前端传入的input参数拷贝到商品属性实体
        BeanUtils.copyProperties(GoodProps,input);

        //调用数据库的增加或者修改方法
        saveOrUpdate(GoodProps);

        //定义一个返回给前端的商品属性传输模型
        GoodPropsDto GoodPropsDto=new GoodPropsDto();

        //同理把操作的商品属性实体拷贝给商品属性传输模型
        BeanUtils.copyProperties(GoodPropsDto,GoodProps);

        //把传输模型返回给前端
        return GoodPropsDto;
    }



}
